var targetId;
var dialogId;

/**
 * 添加：将待选择用户添加到目标用户列表
 */
function addwaitUserToTargetUser(){
	var checkedRows = $("#waitUserDatagrid").datagrid("getChecked");
	if(checkedRows.length > 0){
		for(var i = 0; i < checkedRows.length; i++){
			var rowStr = '{ "ID": "' + checkedRows[i].ID + '","NAME": "'
					+ checkedRows[i].NAME + '","DEPT_NAME": "'
					+ checkedRows[i].DEPT_NAME + '","ROLE_NAME": "'
					+ checkedRows[i].ROLE_NAME + '"}'; 
			var row = JSON.parse(rowStr);
			var rowData = $('#targetUserDatagrid').datagrid("getRows");//获取目标页的数据行
			var boolean = true;
			//判断目标页是否已存在要添加的用户，若有，boolean为false，不执行添加操作
			for(var j = 0; j < rowData.length; j++){
				if(rowData[j]['ID'] == checkedRows[i].ID){
					boolean = false;
					//$.messager.alert("提示", "已添加用户不能选择！", "warning");
					break;
				}
			}
			if(boolean){
				var rowIndex = $('#targetUserDatagrid').datagrid("getRows").length;
				$('#targetUserDatagrid').datagrid('insertRow',{
					index: rowIndex,
					row: row
				});
			}
			
		}
		//添加条目后删除对应的待选择条目
//		for(var i = 0; i < checkedRows.length; i++){
//			var index = $("#waitUserDatagrid").datagrid("getRowIndex",checkedRows[i]);
//			$("#waitUserDatagrid").datagrid("deleteRow",index);
//		}
	}
	else{
		//commonLang.tip.alert替换'提示',commonLang.optip.add.selectUser替换'请选择用户！'
		$.messager.alert(commonLang.tip.alert, commonLang.optip.add.selectUser, 'warning');
	}
}

/**
 * 保存复制到目标用户
 */
function saveCopyToTargetUser(){
	var allRows = $("#targetUserDatagrid").datagrid("getRows");
	if(allRows.length > 0){
		MaskUtil.mask();
		var ids = []; 
		for(var i = 0; i < allRows.length; i++){
			ids.push(allRows[i].ID);
		}
		$.ajax({
			url : 'platform/UserAuthSetController/saveCopyToTargetUser',
			data : {
				targetId : targetId,
				targetUserIds : ids.join(',')
				},
			type : 'post',
			dataType : 'json',
			success : function(result) {
				MaskUtil.unmask();
				parent.copyAfter(dialogId, result.flag);
			}
		});
	}
	else{
		//commonLang.tip.alert替换'提示',commonLang.optip.save.noRecord替换'没有要保存的记录！'
		$.messager.alert(commonLang.tip.alert, commonLang.optip.save.noRecord, 'warning');
	}
}

/**
 * 删除已选择对象
 */
function deleteCopyToTargetUser(){
	var checkedRow = $("#targetUserDatagrid").datagrid("getChecked");//复选框选中的所有行
	if(checkedRow.length > 0){
		for(var i = 0; i < checkedRow.length; i++){
			var rowIndex = $("#targetUserDatagrid").datagrid("getRowIndex",checkedRow[i]);//获取单个选中的行号
			$("#targetUserDatagrid").datagrid("deleteRow",rowIndex);//删除该行
		}
	}else{
		//commonLang.tip.alert替换'提示',commonLang.optip.del.noRecord替换'没有要删除的记录！'
		$.messager.alert(commonLang.tip.alert, commonLang.optip.del.noRecord, 'warning');
	}
}

/**
 * 返回
 */
function closeWindow(){
	if (parent != null && parent.$('#' + dialogId) != null) {
		parent.$('#' + dialogId).dialog('close');
	}
}

function openSearchDialog() {
	$('#searchDialog').css('display','block').dialog({title: commonLang.tip.search});
	$('#searchDialog').dialog('open', true);
}

function searchUserDatas() {
	$('#waitUserDatagrid').datagrid('load', {
		param: JSON.stringify(serializeObject($('#searchForm').form()))
	});
}

function clearData() {
	$('#searchForm').form('clear');
}

function closeSearch() {
	$('#searchDialog').dialog('close', true);
}

//选择部门
function authSelectDept() {
	var deptSelector = new CommonSelector("dept", "deptSelectCommonDialog", "authSelectedDeptId", "deptNameAlias", null, null, null, false, null, null, 600, 400);
	deptSelector.init(false, "selectDeptDialogCallBack", 'n');
}
//部门选择回调
function selectDeptDialogCallBack(data) {
	if(data.length > 0){
		var ids = []; 
		var names = [];
		for(var i = 0; i < data.length; i++){
			ids.push(data[i].deptId);
			names.push(data[i].deptName);
		}
		$("#authSelectedDeptId").val(ids.join(','));
		$("#deptNameAlias").val(names.join(','));
	}else{
		$("#authSelectedDeptId").val("");
		$("#deptNameAlias").val("");
	}
}
//角色
function authSelectRole() {
	var roleSelector = new CommonSelector("role", "roleSelectCommonDialog", "authSelectedRoleId", "roleNameAlias", null, null, null, false, null, null, 600, 400);
	roleSelector.init(false, "selectRoleDialogCallBack", 'n');
}
function selectRoleDialogCallBack(data) {
	if(data.length > 0){
		var ids = []; 
		var names = [];
		for(var i = 0; i < data.length; i++){
			ids.push(data[i].roleId);
			names.push(data[i].roleName);
		}
		$("#authSelectedRoleId").val(ids.join(','));
		$("#roleNameAlias").val(names.join(','));
	}else{
		$("#authSelectedRoleId").val("");
		$("#roleNameAlias").val("");
	}
}
//岗位
function authSelectPosition() {
	var positionSelector = new CommonSelector("position", "positionSelectCommonDialog", "authSelectedPositionId", "positionNameAlias", null, null, null, false, null, null, 600, 400);
	positionSelector.init(false, "selectPositionDialogCallBack", 'n');
}
function selectPositionDialogCallBack(data) {
	if(data.length > 0){
		var ids = []; 
		var names = [];
		for(var i = 0; i < data.length; i++){
			ids.push(data[i].positionId);
			names.push(data[i].positionName);
		}
		$("#authSelectedPositionId").val(ids.join(','));
		$("#positionNameAlias").val(names.join(','));
	}else{
		$("#authSelectedPositionId").val("");
		$("#positionNameAlias").val("");
	}
}